import { defineStore } from 'pinia'
import { i18n } from '@/locales'
import { type Locale, LOCALES } from '@/locales'

export const useLocaleStore = defineStore('locale', {
  state: () => ({
    currentLocale: 'zh-CN' as Locale,
  }),

  getters: {
    primevueLocale(): any {
      return LOCALES[this.currentLocale]
    },
  },

  actions: {
    async setLocale(lang: Locale) {
      // const { usePrimeVue } = await import('primevue/config')
      // const primevue = usePrimeVue()

      this.currentLocale = lang
      i18n.global.locale.value = lang
      //localStorage.setItem('locale', lang)
      //   primevue.config.locale = LOCALES[lang].primevue
      // const primevue = usePrimeVue()
      //primevue.config.locale.accept = "Aceptar";

      // 发布一个自定义事件，让组件处理 PrimeVue 的更新
      window.dispatchEvent(new CustomEvent('localeChange', { detail: lang }))
    },
  },
  persist: {
    pick: ['currentLocale'],
  },
})
